package com.ruoyi.web.controller.system;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Quantify;
import com.ruoyi.system.service.IQuantifyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 计算外Controller
 *
 * @author ruoyi
 * @date 2024-09-01
 */
@RestController
@RequestMapping("/system/quantify")
public class QuantifyController extends BaseController {
    @Resource
    private IQuantifyService quantifyService;

    /**
     * 查询计算外列表
     */
    //@PreAuthorize("@ss.hasPermi('system:quantify:list')")
    @GetMapping("/list")
    public TableDataInfo list(Quantify quantify) {
        startPage();
        List<Quantify> list = quantifyService.selectQuantifyList(quantify);
        return getDataTable(list);
    }

    /**
     * 导出计算外列表
     */
    //@PreAuthorize("@ss.hasPermi('system:quantify:export')")
    @Log(title = "计算外", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Quantify quantify) {
        List<Quantify> list = quantifyService.selectQuantifyList(quantify);
        ExcelUtil<Quantify> util = new ExcelUtil<Quantify>(Quantify.class);
        util.exportExcel(response, list, "计算外数据");
    }

    /**
     * 获取计算外详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:quantify:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(quantifyService.selectQuantifyById(id));
    }

    /**
     * 新增计算外
     */
    //@PreAuthorize("@ss.hasPermi('system:quantify:add')")
    @Log(title = "计算外", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Quantify quantify) {
        return toAjax(quantifyService.insertQuantify(quantify));
    }

    /**
     * 修改计算外
     */
    //@PreAuthorize("@ss.hasPermi('system:quantify:edit')")
    @Log(title = "计算外", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Quantify quantify) {
        return toAjax(quantifyService.updateQuantify(quantify));
    }

    /**
     * 删除计算外
     */
    //@PreAuthorize("@ss.hasPermi('system:quantify:remove')")
    @Log(title = "计算外", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(quantifyService.deleteQuantifyByIds(ids));
    }

    /**
     * 算量
     */
    @PostMapping("/compute")
    @Anonymous
    public AjaxResult compute(String weekStart, String weekEnd) {
//        String weekStart = "2024-09-30";
//        String weekEnd = "2024-10-06";
        quantifyService.compute(weekStart, weekEnd);
        return AjaxResult.success();
    }
}
